package com.jingdianjichi.subject.handler.subject;

import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.common.enums.isDeletedFlagEnum;
import com.jingdianjichi.subject.converter.MultipleSubjectConverter;
import com.jingdianjichi.subject.eneity.SubjectAnswerBO;
import com.jingdianjichi.subject.eneity.SubjectInfoBO;
import com.jingdianjichi.subject.eneity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题枚举类
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{
    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        LinkedList<SubjectMultiple> multipleLinkedList = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(subjectMultiple ->{
            SubjectMultiple subjectMultiple1 = MultipleSubjectConverter.INSTANCE.converterBoToCategory(subjectMultiple);
            subjectMultiple1.setSubjectId(subjectInfoBO.getId());
            subjectMultiple1.setIsDeleted(isDeletedFlagEnum.UN_DELETED.getCode());
            multipleLinkedList.add(subjectMultiple1);
        });
        subjectMultipleService.batchInsert(multipleLinkedList);


    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(Long.valueOf(subjectId));
        List<SubjectMultiple> subjectMultipleList =  subjectMultipleService.batchQuery(subjectMultiple);
        List<SubjectAnswerBO> subjectAnswerBOS = MultipleSubjectConverter.INSTANCE.converterEntitysToAnswers(subjectMultipleList);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }
}
